namespace CISLib.EncryptClasses
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    public class DESEncrypt
    {
        private DES des = new DESCryptoServiceProvider();
        private Encoding encoding = new UnicodeEncoding();
        private string iv = "12345678";
        private string key = "12345678";

        public string Decrypt(string pToDecrypt, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < (pToDecrypt.Length / 2); x++)
            {
                int i = Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 0x10);
                inputByteArray[x] = (byte) i;
            }
            des.Key = Encoding.ASCII.GetBytes(sKey);
            des.IV = Encoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            return Encoding.Default.GetString(ms.ToArray());
        }

        public void DecryptFile(string filePath)
        {
            this.DecryptFile(filePath, filePath);
        }

        public void DecryptFile(string filePath, string outPath)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("指定的解密文件没有找到");
            }
            byte[] ivb = Encoding.ASCII.GetBytes(this.iv);
            byte[] keyb = Encoding.ASCII.GetBytes(this.EncryptKey);
            FileInfo file = new FileInfo(filePath);
            byte[] deCrypted = new byte[file.Length];
            FileStream fin = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            try
            {
                ICryptoTransform decryptor = this.des.CreateDecryptor(keyb, ivb);
                new CryptoStream(fin, decryptor, CryptoStreamMode.Read).Read(deCrypted, 0, deCrypted.Length);
            }
            catch (Exception err)
            {
                throw new ApplicationException(err.Message);
            }
            finally
            {
                try
                {
                    fin.Close();
                }
                catch
                {
                }
            }
            FileStream fout = new FileStream(outPath, FileMode.Create, FileAccess.Write);
            fout.Write(deCrypted, 0, deCrypted.Length);
            fout.Close();
        }

        public string DecryptString(string str)
        {
            byte[] ivb = Encoding.ASCII.GetBytes(this.iv);
            byte[] keyb = Encoding.ASCII.GetBytes(this.EncryptKey);
            byte[] toDecrypt = this.EncodingMode.GetBytes(str);
            byte[] deCrypted = new byte[toDecrypt.Length];
            ICryptoTransform deCryptor = this.des.CreateDecryptor(keyb, ivb);
            MemoryStream msDecrypt = new MemoryStream(toDecrypt);
            CryptoStream csDecrypt = new CryptoStream(msDecrypt, deCryptor, CryptoStreamMode.Read);
            try
            {
                csDecrypt.Read(deCrypted, 0, deCrypted.Length);
            }
            catch (Exception err)
            {
                throw new ApplicationException(err.Message);
            }
            finally
            {
                try
                {
                    msDecrypt.Close();
                    csDecrypt.Close();
                }
                catch
                {
                }
            }
            return this.EncodingMode.GetString(deCrypted);
        }

        public string Encrypt(string pToEncrypt, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
            des.Key = Encoding.ASCII.GetBytes(sKey);
            des.IV = Encoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            ret.ToString();
            return ret.ToString();
        }

        public void EncryptFile(string filePath)
        {
            this.EncryptFile(filePath, filePath);
        }

        public void EncryptFile(string filePath, string outPath)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("没有找到指定的文件");
            }
            byte[] ivb = Encoding.ASCII.GetBytes(this.iv);
            byte[] keyb = Encoding.ASCII.GetBytes(this.EncryptKey);
            FileStream fin = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            string dataStr = new StreamReader(fin, this.EncodingMode).ReadToEnd();
            byte[] toEncrypt = this.EncodingMode.GetBytes(dataStr);
            fin.Close();
            FileStream fout = new FileStream(outPath, FileMode.Create, FileAccess.Write);
            ICryptoTransform encryptor = this.des.CreateEncryptor(keyb, ivb);
            CryptoStream csEncrypt = new CryptoStream(fout, encryptor, CryptoStreamMode.Write);
            try
            {
                csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
                csEncrypt.FlushFinalBlock();
            }
            catch (Exception err)
            {
                throw new ApplicationException(err.Message);
            }
            finally
            {
                try
                {
                    fout.Close();
                    csEncrypt.Close();
                }
                catch
                {
                }
            }
        }

        public string EncryptString(string str)
        {
            byte[] ivb = Encoding.ASCII.GetBytes(this.iv);
            byte[] keyb = Encoding.ASCII.GetBytes(this.EncryptKey);
            byte[] toEncrypt = this.EncodingMode.GetBytes(str);
            ICryptoTransform encryptor = this.des.CreateEncryptor(keyb, ivb);
            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
            csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
            csEncrypt.FlushFinalBlock();
            byte[] encrypted = msEncrypt.ToArray();
            csEncrypt.Close();
            msEncrypt.Close();
            return this.EncodingMode.GetString(encrypted);
        }

        private static byte[] StringToByte(string str, int count)
        {
            byte[] b = new byte[count];
            for (int i = 0; i < count; i++)
            {
                b[i] = Convert.ToByte(str.ToCharArray(i, 1)[0]);
            }
            return b;
        }

        public Encoding EncodingMode
        {
            get
            {
                return this.encoding;
            }
            set
            {
                this.encoding = value;
            }
        }

        public string EncryptKey
        {
            get
            {
                return this.key;
            }
            set
            {
                this.key = value;
            }
        }
    }
}

